<template>
    <div class="cmdcfg">
        <exInput v-model.number="x1" label="x1" width="75" size="mini"></exInput>
        <exInput v-model.number="y1" label="y1" width="75" size="mini"></exInput>
        <exInput v-model.number="rectcount" label="次数" width="120" size="mini"></exInput>
        执行次数:{{runIdx}}
        <br/>
        <exInput v-model.number="x2" label="x2" width="75" size="mini"></exInput>
        <exInput v-model.number="y2" label="y2" width="75" size="mini"></exInput>
        <exInput v-model.number="time" label="用时(毫秒)" width="120" size="mini"></exInput>
    </div>
</template>


<script>

export default {
    props: {
        cmdInfo: { type: Object, default: {} },
        devInfo:{ type: Object, default: {} },
    },
    data() {
        return {
            x1 : this.cmdInfo.x1 || "",
            y1 : this.cmdInfo.y1 || "",
            x2 : this.cmdInfo.x2 || "",
            y2 : this.cmdInfo.y2 || "",
            time : this.cmdInfo.time || 300,
            rectcount : this.cmdInfo.rectcount || 10,
            isStop : false,
            runIdx : 0,
        };
    },
    inject: ['doDevCmd','doNextCmd'],
     

    mounted() {
    },

    methods: {
        setByDevInfo(){
            this.x1 = this.devInfo.clickX || "";
            this.y1 = this.devInfo.clickY || "";
            this.x2 = this.devInfo.clickRX || "";
            this.y2 = this.devInfo.clickRY || "";
        },
        saveCmd(){
            this.cmdInfo.x1 = this.x1;
            this.cmdInfo.y1 = this.y1;
            this.cmdInfo.x2 = this.x2;
            this.cmdInfo.y2 = this.y2;
            this.cmdInfo.time = this.time;
            this.cmdInfo.rectcount = this.rectcount;
        },
        preCheck(){
            if(!this.x1){
                this.$message.error("请输入X坐标")
                return false;
            }
            if(!this.y1){
                this.$message.error("请输入Y坐标")
                return false;
            }
            if(!this.x2){
                this.$message.error("请输入X2坐标")
                return false;
            }
            if(!this.y2){
                this.$message.error("请输入Y2坐标")
                return false;
            }
            if(!this.time){
                this.$message.error("请输入用时")
                return false;
            }
            if(!this.rectcount){
                this.$message.error("请输入次数")
                return false;
            }
            return true;
        },
        stopCmd(){
            this.isStop = true;
        },
        getRandom(a,b){
            var min = Math.min(a,b);
            var max = Math.max(a,b);
            return Math.floor(Math.random() * (max - min + 1)) + min
        },
        runCmd(isOnce){
            if(!this.preCheck()){
                this.$emit("stopRun")
                return;
            }
            this.isStop = false;
            this.runIdx = 0;
            this.doMove(isOnce);

        },
        doMove(isOnce){
            var x1 = this.getRandom(this.x1,this.x2)
            var x2 = this.getRandom(this.x1,this.x2)
            var y1 = this.getRandom(this.y1,this.y2)
            var y2 = this.getRandom(this.y1,this.y2)
            this.preView(x1,y1,x2,y2)
            this.doDevCmd(`adb shell input swipe ${x1*this.devInfo.devBet} ${y1*this.devInfo.devBet} ${x2*this.devInfo.devBet} ${y2*this.devInfo.devBet} ${this.time}`);
            this.runIdx++;
            if(this.runIdx>=this.rectcount || this.isStop){
                setTimeout(()=>{
                    this.$emit("stopRun")
                    if(isOnce!==true) this.doNextCmd();
                },this.time)
                return;
            }
            setTimeout(()=>{
                this.doMove(isOnce)
            },this.time)
        },
        preView(x1,y1,x2,y2){
            this.devInfo.clickX = x1 || this.x1;
            this.devInfo.clickY = y1 || this.y1;
            this.devInfo.clickRX = x2 || this.x2;
            this.devInfo.clickRY = y2 || this.y2;
        }
    }
}
</script>

<style lang="less" scoped>
    .cmdcfg{
        display: inline-block;
    }
</style>
